package Stream;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

public class MapInput extends JInternalFrame {

	HashMap<String , MapData> map; //HashMap 선언
	
	private JLabel lblId;
	private JLabel lblNewLabel;
	private JLabel lblPhone;
	private JTextField userId;
	private JTextField irum;
	private JTextField phone;
	private JButton btnNewButton;
	private JLabel status;

	/**
	 * Create the frame.
	 */
	public MapInput(HashMap<String, MapData> m) { //매개변수없는 생성자에서 매개변수있는생성자로 변경
		super("Input",true, true, true, false);
		this.map = m;//매개변수로받은 HashMap을 필드에있는 HashMap에 저장
		setBounds(100, 100, 333, 144);
		getContentPane().setLayout(null);
		getContentPane().add(getLblId());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblPhone());
		getContentPane().add(getUserId());
		getContentPane().add(getIrum());
		getContentPane().add(getPhone());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getStatus());

	}
	
	public void setMap(HashMap m) {
		this.map = m;
	}
	
	private JLabel getLblId() {
		if (lblId == null) {
			lblId = new JLabel("ID");
			lblId.setBounds(12, 10, 57, 15);
		}
		return lblId;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("NAME");
			lblNewLabel.setBounds(12, 35, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblPhone() {
		if (lblPhone == null) {
			lblPhone = new JLabel("PHONE");
			lblPhone.setBounds(12, 60, 57, 15);
		}
		return lblPhone;
	}
	private JTextField getUserId() {
		if (userId == null) {
			userId = new JTextField();
			userId.setBounds(81, 7, 116, 21);
			userId.setColumns(10);
		}
		return userId;
	}
	private JTextField getIrum() {
		if (irum == null) {
			irum = new JTextField();
			irum.setBounds(81, 32, 116, 21);
			irum.setColumns(10);
		}
		return irum;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setBounds(81, 57, 116, 21);
			phone.setColumns(10);
		}
		return phone;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String str = userId.getText();
					if (map.get(str) == null) {//빈칸 체크
						boolean flag = true;
						if (userId.getText().equals("")) {
							status.setText("아이디를 입력하세요");
							flag = false;
						} else if (irum.getText().equals("")) {
							status.setText("이름를 입력하세요");
							flag = false;
						} else if (irum.getText().equals("")) {
							status.setText("전화번호를 입력하세요");
							flag = false;
						}
						if (flag) {//모든칸이 채워졌을 때 실행
							MapData md = new MapData(str, irum.getText(), phone.getText());
							map.put(userId.getText(), md);// 저장
							userId.setText("");// 공백입력
							irum.setText("");
							phone.setText("");
							status.setText(str + "입력됨");
						}
					}else{
						JOptionPane.showMessageDialog(null, "아이디가 겹칩니다.");
					}
				}
			});
			btnNewButton.setBounds(209, 6, 97, 69);
		}
		return btnNewButton;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("");
			status.setBackground(new Color(176, 224, 230));
			status.setOpaque(true);
			status.setBounds(12, 85, 294, 15);
		}
		return status;
	}
}
